﻿using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ue.Extensions;


namespace Ue.Tests.Extensions
{
    [TestClass]
    public class IEnumableExtensionTests
    {
        #region Foreach

        [TestMethod]
        public void ForeachTests()
        {
            int[] numbers = { 1, 2, 3, 4 };

            int total = 0;
            numbers.Foreach(item=>total+=item);

            Assert.AreEqual(10, total);
        }

        [TestMethod]
        public void ForeachEnumberableIsNull()
        {
            int[] numbers = null;

            int total = 0;

            numbers.Foreach(item=>total+=item);

            Assert.AreEqual(0, total);
        }

        [TestMethod]
        public void ForeachByIndex()
        {
            int[] numbers = { 1, 2, 3, 4 };
            int total = 0;

            numbers.Foreach((item, index) => total += (index > 2 ? item : 0));
            
            Assert.AreEqual(4,total);
        }

        #endregion

        #region CombineToString

        [TestMethod]
        public void CombineToString()
        {
            string[] strs = { "1", "2", "3" };

            Assert.AreEqual("1|2|3", strs.CombineToString());
        }
        [TestMethod]
        public void CombineToStringBySeparator()
        {
            string[] strs = { "1", "2", "3" };

            Assert.AreEqual("1-2-3", strs.CombineToString('-'));
        }

        [TestMethod]
        public void SeparatorIsWhiteSpaceThenThrowException()
        {
            string[] strs = { "1", "2", "3" };
            try
            {
                strs.CombineToString(' ');
                Assert.Fail("错误，分割符为空时应该抛出异常");
            }
            catch(ArgumentNullException){}
        }

        [TestMethod]
        public void CombineToStringByIConvertibleItem()
        {
            int[] numbers = { 1, 2, 3 };
            char[] chars = { '-','^','!'};

            Assert.AreEqual("1|2|3",numbers.CombineToString());
            Assert.AreEqual("-|^|!", chars.CombineToString());
        }

        [TestMethod]
        public void CombineToStringByItemHandler()
        {
            object[] objects = { new { Name = "李" }, new { Name = "绍" }, new { Name = "伟" } };

            Assert.AreEqual("李|绍|伟", objects.CombineToString(delegate(dynamic item)
            {
                return item.Name;
            }));
        }

        [TestMethod]
        public void ItemHandlerIsNullAndItemIsNotIConvertibleThenThrowException()
        {
            object[] objects = { new { Name = "李" }, new { Name = "绍" }, new { Name = "伟" } };

            try
            {
                objects.CombineToString(null);
                Assert.Fail("当对像没有指定ItemHandler委托方法，并且Item对像不支持IConvertible无法转换为字符串时应该抛出异常");
            }
            catch (ArgumentNullException) { }
        }

        #endregion
    }
}
